package com.quasar.hpatient.view.personal.register.registerFragment;

import android.text.Editable;
import android.text.TextWatcher;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.PatientInfoData;
import com.quasar.hpatient.onInterface.viewInterface.UiInterface;
import com.quasar.hpatient.presenter.AloneUserPresenter;
import com.quasar.hpatient.utils.StringUtils;
import com.quasar.hpatient.view.personal.register.RegisterEditActivity;
import com.quasar.hpatient.widget.EditWidget;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;

/**
 * author : 许建宏
 * Date: 2017/11/27 0027
 * View:注册姓名
 */

@EFragment(R.layout.register_name_fragment_layout)
public class RegisterNameFragment extends BaseFragment implements UiInterface<DataStateResult> {


    private AloneUserPresenter aloneUserPresenter;

    @ViewById(R.id.ew_name)
    EditWidget ew_name;

    @FragmentArg
    PatientInfoData patient;

    @AfterViews
    void init(){
        initView();
        initData();
    }

    @Override
    public void initView() {

        if(patient!=null){
            if(patient.getRealName()!=null && patient.getRealName().length()>0){
                if(patient.getRealName().equals(patient.getPhone())){//手机号码和名字相同置空
                    ew_name.setText("");
                }else{
                    ew_name.setText(patient.getRealName());
                }
            }
        }

        ew_name.setLetfTextFwatch(new TextWatcher() {
            String str;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String strs = ew_name.getText().toString();
                str = StringUtils.stringFilter(strs.toString());
                if (!strs.equals(str)) {
                    ew_name.setText(str);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {
        aloneUserPresenter=new AloneUserPresenter(this);

    }

    //提交
    @Click(R.id.tv_next)
    public void tv_next(){
        String name=ew_name.getText().toString().trim();

        if(name==null || name.length()==0){
            msg("请输入名称");
            return;
        }
        showDialog();

        aloneUserPresenter.AloneUserData(ConstantManger.USERNAME,name,ConstantManger.TABLEUSERINFO);
    }
    //跳过
    @Click(R.id.tv_skip)
    public void tv_skip(){
        RegisterEditActivity.ViewPage(1);
    }

    @Override
    public void onViewSuccess(DataStateResult data) {
        if(data.getMsgCode().equals("0x00000000")){
            RegisterEditActivity.ViewPage(1);
        }
        dessDialog();
    }

    @Override
    public void onViewError(String msg) {
        if(msg!=null){
            msg(msg);
        }
        dessDialog();
    }

    @Override
    public void onViewRefreshUI() {

    }

}
